﻿using UnityEngine;

namespace E.Tool
{
    public class Bullet : MonoBehaviour
    {
        [Header("运动轨迹")]
        public float moveSpeed = 1;    // 单位：格
        public float rotateSpeed = 0;  // 单位：度
        public float lifetime = 1;     // 单位：秒

        [Header("碰撞模式")]
        public HitMode hitMode = HitMode.Once;
        public float hitInterval = 1;

        [Header("碰撞效果")]
        public float buff;

        [SerializeField] private new Rigidbody2D rigidbody2D;

        public enum HitMode
        {
            None,
            Once,
            Multi,
            Persistent
        }

        public void Init()
        {
            gameObject.SetActive(true);
        }

        private void OnEnable()
        {
            // 倒计时死亡
            Invoke(nameof(Die), lifetime);
        }

        private void Update()
        {
            // 检测是否需要持续旋转
            if(rotateSpeed != 0)
            {
                transform.Rotate(0, 0, rotateSpeed * Time.deltaTime);
            }

            // 获取当前朝向
            Vector2 dir = (transform.rotation * Vector2.right).normalized;
            //Debug.Log(dir);

            // 设置当前速度
            rigidbody2D.linearVelocity = dir * moveSpeed;
        }

        private void OnDisable()
        {
            Destroy(gameObject);
        }

        private void OnTriggerEnter2D(Collider2D collision)
        {
            Debug.Log("击中对象：" + collision.gameObject.name);
            Die();
        }

        public void Die()
        {
            //Destroy(gameObject);

            gameObject.SetActive(false);
        }
    }
}